﻿using DreamCMS.BLL;
using DreamCMS.Entity;
using DreamCMS.Entity.Common;
using DreamCMS.WebCore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamCMS.Web
{
    public partial class Category : FrontPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitCurrentTemplateContext();//初始化当前页面的模板标签上下文对象
            Display("category.html"); //展现页面
        }


        /// <summary>
        /// 初始化模板引擎辅助类
        /// 使用相关标签数据初始化模板辅助类中的NVelocity模板的标签上下文对象
        /// </summary>
        private void InitCurrentTemplateContext()
        {
            tplHelper.Put(DmTagsSet.IS_DEFAULT, "0");  //设置首页标识标签，用于在模板中进行首页判断
            tplHelper.Put(DmTagsSet.POST_MESSAGE, string.Empty); //页面信息，比如可以显示当前页面的Position,显示当前页面是搜索结果页，或者标签列表页或者作者文章列表页

            //文章页列表（首页）
            long categoryId = -1;
            long tagId = -1;
            long userId = -1;
            string keyword = string.Empty;
            string data = string.Empty;
            string begindate = string.Empty;
            string enddate = string.Empty;
            long recordCount = 0;
            string messageinfo = string.Empty;
            string url = string.Empty;
            long pageindex = RequestHelper.QueryInt("page", 1); //获取页码
            string name = RequestHelper.GetQueryString("cat", true);//获取栏目名
            tplHelper.Put(DmTagsSet.PAGE_TITLE, name); //设置模板页面的title标题
            string pageUrl = RequestHelper.SiteUrl + "category.aspx?cat=" + name + "&page={0}";
            CategoryInfo cate = null;
            if (StringHelper.IsUnsignedNum(name))
            {
                cate = CategoryManager.GetCatById(StringHelper.StrToInt64(name, 0));
            }
            else
            {
                cate = CategoryManager.GetCategoryBySlug(name);
            }

            if (cate != null)
            {
                categoryId = cate.CatId;
                tplHelper.Put(DmTagsSet.META_KEYWORDS, cate.Name);
                tplHelper.Put(DmTagsSet.META_DESCRIPTION, cate.Description);
                tplHelper.Put(DmTagsSet.PAGE_TITLE, cate.Name);
                messageinfo = string.Format("<h2 class=\"post-message\">分类:{0}</h2>", cate.Name);

                tplHelper.Put(DmTagsSet.POSTS, PostManager.GetPostList(SiteSettingManager.GetSiteSettingInfo().PageSizePostCount, pageindex, out recordCount, categoryId, tagId, userId, PostRecommend.Other,PostStatus.Published,PostTopStatus.Other, PostHideStatus.NoHide, begindate, enddate, keyword));
                string pageHtml = Pager.CreateHtml(SiteSettingManager.GetSiteSettingInfo().PageSizePostCount, recordCount, pageUrl);
                tplHelper.Put(DmTagsSet.PAGER, pageHtml);
                tplHelper.Put(DmTagsSet.CATEID, categoryId);
            }
        }
    }
}